package com.demo.tran;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@Service
public class TransactionMessageService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendTransactionMessage(String topic, String businessKey,String  mes) {
        // 构建消息
        Message<String> message = MessageBuilder
            .withPayload(mes)
            .setHeader("KEYS", businessKey) // 设置消息KEY，用于事务回查
            .build();

        // 发送事务消息
        rocketMQTemplate.sendMessageInTransaction(
            topic,
            message,
            null // 可传递额外参数给 listener
        );
    }

}